package com.ruoyi.warning.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.warning.domain.entity.Message;
import com.ruoyi.warning.domain.query.MessageQuery;
import com.ruoyi.warning.mapper.MessageMapper;
import com.ruoyi.warning.service.IMessageService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 告警信息记录表 服务实现类
 * </p>
 *
 * @author chenhc
 * @since 2025-02-06
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements IMessageService {

    @Override
    public TableDataInfo getMessagePage(MessageQuery query) {
        Page<Message> page = PageHelper.startPage(query.getPageNum(),query.getPageSize());
        LambdaQueryWrapper<Message> wrapper = new QueryWrapper<Message>().lambda()
                .eq(query.getAreaId()!=null,Message::getAreaId,query.getAreaId())
                .eq(query.getWarningWay()!=null,Message::getWarningWay,query.getWarningWay())
                .eq(query.getWarningLevel()!=null,Message::getWarningLevel,query.getWarningLevel())
                .eq(query.getWarningType()!=null,Message::getWarningType,query.getWarningType())
                .ge(query.getStartTime()!=null,Message::getCreateTime,query.getStartTime())
                .le(query.getEndTime()!=null,Message::getCreateTime,query.getEndTime()+" 23:59:59")
                .like(query.getContactName()!=null,Message::getWarningContact,query.getContactName())
                .orderByDesc(Message::getCreateTime);
        List<Message> list = this.list(wrapper);
        TableDataInfo tableDataInfo = new TableDataInfo(list, page.getTotal());
        return tableDataInfo;
    }

    @Override
    public List<Message> getMessageList(MessageQuery query) {
        LambdaQueryWrapper<Message> wrapper = new QueryWrapper<Message>().lambda()
                .eq(Message::getAreaId,query.getAreaId())
                .eq(Message::getWarningWay,query.getWarningWay())
                .eq(Message::getWarningLevel,query.getWarningLevel())
                .eq(Message::getWarningType,query.getWarningType())
                .like(Message::getCreateTime,query.getWarningTime())
                .like(Message::getWarningContact,query.getContactName());
        return this.list(wrapper);
    }

    @Override
    public String saveMessage(Message message) {
        this.save(message);
        return null;
    }

    @Override
    public String deleteMessage(List<String> ids) {
        this.removeByIds(ids);
        return null;
    }

    @Override
    public String updateMessage(Message message) {
        this.updateById(message);
        return null;
    }

    @Override
    public String export(List<String> ids) {
        LambdaQueryWrapper<Message> wrapper = new QueryWrapper<Message>().lambda()
                .in(Message::getId,ids);
        List<Message> list = this.list(wrapper);
        ExcelUtil<Message> util = new ExcelUtil<>(Message.class);
        util.exportExcel(list, "Message");
        return null;
    }

    @Override
    public Message getDetails(String id) {
        return this.getById(id);
    }


}
